package com.ceic.hn.efiletransfer

import jakarta.annotation.PreDestroy
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.bind.ConstructorBinding
import org.springframework.core.task.TaskExecutor
import org.springframework.stereotype.Component

@ConfigurationProperties(prefix = "app")
data class TransferProperties @ConstructorBinding constructor(
    val transfers: List<TransferSetting>
)

@Component
class EFileTransferRunner(
    private val properties: TransferProperties,
    private val taskExecutor: TaskExecutor
) : CommandLineRunner {

    lateinit var transfers: List<EFileTransfer>

    override fun run(vararg args: String?) {
        transfers = properties.transfers.map {
            EFileTransfer(it, taskExecutor)
        }
        transfers.forEach { it.start() }
    }

    @PreDestroy
    fun destroy() {
        transfers.forEach {
            it.stop()
        }
    }

}